﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web.UI;

using AjaxControlToolkit;

namespace SCS.Web.UI.WebControls.Dialogs
{
    public static class AjaxHelper
    {
        private static readonly object ReflectionLock = new object();

        public static bool IsAjaxInstalled
        {
            get
            {
                lock (ReflectionLock)
                {
                    Type scriptManagerType =
                       Type.GetType(
                       "System.Web.UI.ScriptManager, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35",
                       false);

                    return (scriptManagerType != null);
                }
            }
        }

        public static ToolkitScriptManager GetScriptManager(Page page)
        {
            return (ToolkitScriptManager)ToolkitScriptManager.GetCurrent(page);
        }

        public static bool IsInAsyncPostBack(Page page)
        {
            ToolkitScriptManager manager = GetScriptManager(page);

            return (manager != null && manager.IsInAsyncPostBack);
        }
        
        public static bool IsControlInPartialRendering(Control control)
        {
            Control parent = control.Parent;

            while (parent != null)
            {
                if (parent is UpdatePanel)
                {
                    if (((UpdatePanel)parent).IsInPartialRendering)
                        return true;

                }
                parent = parent.Parent;
            }

            return false;
        }

        public static void AddScriptReference(Page page, ScriptReference scriptRef)
        {
            ToolkitScriptManager scriptMan = (ToolkitScriptManager)ToolkitScriptManager.GetCurrent(page);

            if (scriptMan != null)
                scriptMan.Scripts.Add(scriptRef);
        }

        public static string EscapeQuotes(string text)
        {
            return text.Replace("'", "\\'");
        }

        public static string SerializeToJson<T>(T instance)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

            string json = "";

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, instance);
                json = Encoding.Default.GetString(ms.ToArray());
            }

            return json;
        }

        public static T DeserializeFromJson<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();

            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

                obj = (T)serializer.ReadObject(ms);
            }
            
            return obj;
        }
    }
}
